package com.wxy.controller;

import com.wxy.entity.GroupUser;
import com.wxy.entity.User;
import com.wxy.entity.ValidInterface.group1;
import com.wxy.entity.ValidInterface.group2;
import com.wxy.service.AsyncTest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 47868
 */
@Controller
@Validated
@RequestMapping("sb03")
@Tag(name = "控制器",description = "这是控制器测试")
public class ControllerTest {
    private AsyncTest asyncTest;
    @Autowired
    public void setAsyncTest(AsyncTest asyncTest) {
        this.asyncTest = asyncTest;
    }
    @RequestMapping("user")
    public String user() {
        return "user";
    }
    @Operation(summary = "测试接口", description = "Valid测试接口")
    @ApiResponse(responseCode = "200",description = "测试成功",content = @Content)
    @PostMapping("/valid")
    @ResponseBody
    public String valid(@Valid @RequestBody User user, BindingResult result) {
        if (result.hasErrors()) {
            result.getFieldErrors().forEach(error -> System.out.println(error.getDefaultMessage()));
            return "error";
        }
        System.out.println(user.toString());
        return "OK";
    }

    @Operation(summary = "接口测试",description = "Validated接口测试")
    @ApiResponse(responseCode = "200",description = "测试成功",content = @Content)
    @PostMapping("/validated")
    @ResponseBody
    public String valid2(@Validated({group1.class, group2.class}) @RequestBody GroupUser user, BindingResult result) {
        System.out.println("Validated测试...");
        if (result.hasErrors()) {
            result.getFieldErrors().forEach(error -> System.out.println(error.getDefaultMessage()));
            return "error";
        }
        System.out.println(user.toString());
        return "OK";
    }

    @Operation(summary = "Async测试",description = "Async测试接口")
    @ApiResponse(responseCode = "200",description = "测试成功",content = @Content)
    @GetMapping("async")
    @ResponseBody
    public String async() {
        System.out.println("这是主线程输出...");
        asyncTest.asyncTest();
        System.out.println("主线程输出完成...");
        return "这是Async测试接口";
    }
}
